package com.yuan.hospital.common.exception;

import com.yuan.hospital.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 * @Author yuan
 * @Date 2021/7/12 15:43
*/
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        log.error("未知异常！原因是:",e);
        return Result.fail();
    }

    @ExceptionHandler(EduException.class)
    @ResponseBody
    public Result error(EduException e){
        log.error("发生业务异常！原因是：{}",e.getMessage());
        return Result.fail(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public Result error(NullPointerException e){
        log.error("发生空指针异常！原因是:",e);
        return Result.fail(400,"请求的数据格式不符!");
    }


}
